package com.health.platform.provider.womenandchildhealth.service;

import com.health.platform.common.core.entity.CommonQueryEntity;
import com.health.platform.common.restful.AppResponse;
import com.health.platform.common.core.entity.EchartsEntity;
import com.health.platform.provider.womenandchildhealth.dao.WomenAndChildHealthDao;
import com.health.platform.provider.womenandchildhealth.entity.SummaryEntity;
import com.health.platform.common.utils.MyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公卫服务-妇幼保健（地方）Service
 *
 * @author shi_xy_sr@neusiri.com
 * @date 2021/3/1 20:47
 */
@Service
public class WomenAndChildHealthService {
    @Resource
    private WomenAndChildHealthDao womenAndChildHealthDao;
    /**
     * 获取中间部分汇总信息 (活产数、孕产妇建档数、儿童管理数、活产性别、健康管理、高危情况)
     *
     * @param 
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/2 8:51
     */
    public AppResponse getSummaryInfo(CommonQueryEntity womenAndChildHealthEntity) {
        Map<String, Object> result = new HashMap<>();
        //——————————————————————活产数、孕产妇建档数、儿童管理数——————————————————————
        SummaryEntity summary = womenAndChildHealthDao.getSummaryInfo(womenAndChildHealthEntity);
        result.put("summary", summary);
        //——————————————————————活产性别比例——————————————————————
        List<EchartsEntity> hcxbRate= womenAndChildHealthDao.getHcxbRate(womenAndChildHealthEntity);
        result.put("hcxbRate", hcxbRate);
        //——————————————————————健康管理——————————————————————
        //孕产妇健康管理
        womenAndChildHealthEntity.setTargetType("FY_TYPE5");
        womenAndChildHealthEntity.setTargetType2("FY_TYPE10");
        EchartsEntity ycfRate= womenAndChildHealthDao.getRate(womenAndChildHealthEntity);
        result.put("ycfRate", ycfRate);
        //儿童健康管理
        womenAndChildHealthEntity.setTargetType("FY_TYPE12");
        womenAndChildHealthEntity.setTargetType2("FY_TYPE11");
        EchartsEntity etRate= womenAndChildHealthDao.getRate(womenAndChildHealthEntity);
        result.put("etRate", etRate);
        //——————————————————————高危情况
        //高危孕产妇
        womenAndChildHealthEntity.setTargetType("FY_TYPE6");
        womenAndChildHealthEntity.setTargetType2("FY_TYPE4");
        EchartsEntity gwycfRate= womenAndChildHealthDao.getRate(womenAndChildHealthEntity);
        result.put("gwycfRate", gwycfRate);
        //高危儿童
        womenAndChildHealthEntity.setTargetType("FY_TYPE14");
        womenAndChildHealthEntity.setTargetType2("FY_TYPE11");
        EchartsEntity gwetRate= womenAndChildHealthDao.getRate(womenAndChildHealthEntity);
        result.put("gwetRate", gwetRate);
        return AppResponse.success("查询成功", result);
    }
    /**
     * 获取根据地区分组的echarts图表数据
     * 
     * @param 
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/3 9:52
     */
    public AppResponse getEchartsInfoByArea(CommonQueryEntity womenAndChildHealthEntity) {
        Map<String, Object> result = new HashMap<>();
        if(womenAndChildHealthEntity.getSgbCode().length() == 2){//省
            womenAndChildHealthEntity.setSubstrIndex(4);
        }else if(womenAndChildHealthEntity.getSgbCode().length() == 4){//市
            womenAndChildHealthEntity.setSubstrIndex(6);
        }else if(womenAndChildHealthEntity.getSgbCode().length() == 6){//区
            womenAndChildHealthEntity.setSubstrIndex(8);
        }
        //早孕建册率
        womenAndChildHealthEntity.setTargetType("FY_TYPE1");
        List<EchartsEntity> earlyPregnancyRate = womenAndChildHealthDao.getEchartsGWByArea(womenAndChildHealthEntity);
        //新生儿访视率
        womenAndChildHealthEntity.setTargetType("FY_TYPE15");
        List<EchartsEntity> newBabyRate = womenAndChildHealthDao.getEchartsGWByArea(womenAndChildHealthEntity);
        result.put("earlyPregnancyRate", earlyPregnancyRate);
        result.put("newBabyRate", newBabyRate);
        return AppResponse.success("查询成功", result);
    }
    /**
     * 根据近五年获取echarts图表数据（活产胎次、五岁以下儿童死亡率、产前筛查情况、孕产妇死亡率）
     *
     * @param
     * @return 结果
     * @author shi_xy_sr@neusiri.com
     * @date 2021/3/4 13:17
     */
    public AppResponse getEchartsInfoByYear(CommonQueryEntity womenAndChildHealthEntity) {
        Map<String, Object> result = new HashMap<>();
        //——————————————————————活产胎次——————————————————————
        womenAndChildHealthEntity.setTargetType("FY_TYPE10");
        //一胎
        womenAndChildHealthEntity.setSpareCode1("1");
        List<EchartsEntity> aliveEveryYearOneMap = womenAndChildHealthDao.getEchartsGWByYear(womenAndChildHealthEntity);
        aliveEveryYearOneMap = MyUtils.getEchartsDataByYear(aliveEveryYearOneMap,womenAndChildHealthEntity.getRecentYearArr());
        result.put("aliveEveryYearOneMap",aliveEveryYearOneMap);
        //二胎
        womenAndChildHealthEntity.setSpareCode1("2");
        List<EchartsEntity> aliveEveryYearTwoMap = womenAndChildHealthDao.getEchartsGWByYear(womenAndChildHealthEntity);
        aliveEveryYearTwoMap = MyUtils.getEchartsDataByYear(aliveEveryYearTwoMap,womenAndChildHealthEntity.getRecentYearArr());
        result.put("aliveEveryYearTwoMap",aliveEveryYearTwoMap);
        //——————————————————————五岁以下儿童死亡率——————————————————————
        womenAndChildHealthEntity.setSpareCode1(null);
        //活产胎次
        List<EchartsEntity> birthMap = womenAndChildHealthDao.getEchartsGWByYear(womenAndChildHealthEntity);
        //五岁以下儿童死亡数
        womenAndChildHealthEntity.setTargetType("FY_TYPE16");
        List<EchartsEntity> minfiveDealMap = womenAndChildHealthDao.getEchartsGWByYear(womenAndChildHealthEntity);
        //五岁以下儿童死亡率 = 五岁以下儿童死亡数/活产胎次
        List<EchartsEntity> minfiveDealRate = MyUtils.getEchartsDataByListByYear(minfiveDealMap,birthMap,womenAndChildHealthEntity.getRecentYearArr());
        result.put("minfiveDealRate",minfiveDealRate);
        //——————————————————————产前筛查情况——————————————————————
        //产妇数
        womenAndChildHealthEntity.setTargetType("FY_TYPE3");
        List<EchartsEntity> gravidaMap = womenAndChildHealthDao.getEchartsGWByYear(womenAndChildHealthEntity);
        //产前筛查数
        womenAndChildHealthEntity.setTargetType("FY_TYPE7");
        List<EchartsEntity> gravidaCheckMap = womenAndChildHealthDao.getEchartsGWByYear(womenAndChildHealthEntity);
        gravidaCheckMap = MyUtils.getEchartsDataByYear(gravidaCheckMap,womenAndChildHealthEntity.getRecentYearArr());
        result.put("gravidaCheckMap",gravidaCheckMap);
        //产前筛查率 = 产前筛查数/产妇数
        List<EchartsEntity> gravidaCheckRate = MyUtils.getEchartsDataByList(gravidaCheckMap,gravidaMap);
        gravidaCheckRate = MyUtils.getEchartsDataByYear(gravidaCheckRate,womenAndChildHealthEntity.getRecentYearArr());
        result.put("gravidaCheckRate",gravidaCheckRate);
        //产前筛查高危数
        womenAndChildHealthEntity.setTargetType("FY_TYPE8");
        List<EchartsEntity> gravidaCheckDangerMap = womenAndChildHealthDao.getEchartsGWByYear(womenAndChildHealthEntity);
        gravidaCheckDangerMap = MyUtils.getEchartsDataByYear(gravidaCheckDangerMap,womenAndChildHealthEntity.getRecentYearArr());
        result.put("gravidaCheckDangerMap",gravidaCheckDangerMap);
        //产前筛查高危率 = 产前筛查高危数/产妇数
        List<EchartsEntity> gravidaCheckDangerRate = MyUtils.getEchartsDataByList(gravidaCheckDangerMap,gravidaMap);
        gravidaCheckDangerRate = MyUtils.getEchartsDataByYear(gravidaCheckDangerRate,womenAndChildHealthEntity.getRecentYearArr());
        result.put("gravidaCheckDangerRate",gravidaCheckDangerRate);
        //——————————————————————孕产妇死亡率——————————————————————
        //孕产妇死亡数
        womenAndChildHealthEntity.setTargetType("FY_TYPE9");
        List<EchartsEntity> gravidaDealMap = womenAndChildHealthDao.getEchartsGWByYear(womenAndChildHealthEntity);
        //孕产妇死亡率 = 孕产妇死亡数/活产胎次
        List<EchartsEntity> gravidaDealRate = MyUtils.getEchartsDataByListByYear(gravidaDealMap,birthMap,womenAndChildHealthEntity.getRecentYearArr());
        result.put("gravidaDealRate",gravidaDealRate);
        return AppResponse.success("查询成功", result);
    }
}
